package com.k12.admim.service.impl;

import com.k12.admim.model.entity.sys.SysMenu;
import com.k12.admim.model.entity.sys.SysRole;
import com.k12.admim.model.vo.MenuVo;
import com.k12.admim.rpc.UserFeignClient;
import com.k12.admim.service.OperatorMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OperatorMenuServiceImpl implements OperatorMenuService {


    @Resource
    private UserFeignClient userFeignClient;

    @Override
    public List<MenuVo> selectMenuList(String tenantId, String userId) {
        List<MenuVo> menuList = null;
        // 管理员显示所有菜单信息
        if ("1".equals(userId)) {
            menuList = userFeignClient.selectMenuList(tenantId);
        } else {
            menuList = userFeignClient.selectMenuListByUserId(tenantId,userId);
        }
        return menuList;

    }

    @Override
    public List<Long> selectMenuListByRoleId(Long roleId,String tenantId) {
        SysRole sysRole = userFeignClient.selectOneRole(roleId, tenantId);
        List<Long> menuList = userFeignClient.selectMenuListByRoleId(roleId, sysRole.getMenuCheckStrictly() == 1,tenantId);
        return menuList;
    }

    @Override
    public boolean checkMenuNameUnique(SysMenu menu) {
        Long menuId = menu.getMenuId() != null ? -1L : menu.getMenuId();
        SysMenu info = userFeignClient.checkMenuNameUnique(menu.getMenuName(), menu.getTenantId(), menu.getParentId());
        if (info != null && info.getMenuId().longValue() != menuId.longValue())
        {
            return false;
        }
        return true;
    }

    @Override
    public int insertMenu(SysMenu menu) {

        return userFeignClient.insertMenu(menu);
    }

    @Override
    public SysMenu selectMenuById(Long menuId,String tenantId) {
        return userFeignClient.selectMenuById(menuId,tenantId);
    }

    @Override
    public SysMenu updateMenu(SysMenu menu) {
        return userFeignClient.updateMenu(menu);
    }

    @Override
    public Boolean hasChildByMenuId(Long menuId,String tenantId) {
        int result = userFeignClient.hasChildByMenuId(menuId,tenantId);
        return result > 0;
    }

    @Override
    public Boolean checkMenuExistRole(Long menuId,String tenantId) {
        int result = userFeignClient.checkMenuExistRole(menuId,tenantId);
        return result > 0;
    }

    @Override
    public Boolean deleteMenuById(Long menuId,String tenantId) {
        return userFeignClient.deleteMenuById(menuId,tenantId);
    }
}
